/*
 * 
 * 
 */
package de.hpi.generator.helpers;

import java.util.ArrayList;
import java.util.Random;

/**
 *
 * @author Michael Wolowyk
 */
public class RandomSelector {

    protected final int PAGESIZE = 10000;
    ArrayList<Integer> aValuesArray = new ArrayList<Integer>(PAGESIZE);
    Random random = new Random();
    int aStartValue = 0;
    private int aValuesRange = PAGESIZE;

    public RandomSelector() {
    }

    public void calibrate(int aStartValue, int aValuesRange) {
        aValuesArray.clear();
        this.aStartValue = aStartValue;
        this.aValuesRange = aValuesRange;

    }

    public int getRandomValue() throws Exception {
        if (aValuesArray.isEmpty()) {
            fillArrayWithValues();
        }

        int aIndex = random.nextInt(aValuesArray.size());
        int result = aValuesArray.get(aIndex);
        aValuesArray.remove(aIndex);

        return result;
    }

    private void fillArrayWithValues() throws Exception {
        int aAcctualPageSize = (aValuesRange < PAGESIZE) ? aValuesRange : PAGESIZE;

        if (aAcctualPageSize == 0) {
            throw new Exception("a array range is going to end");
        }

        for (int aIndex = 0; aIndex < aAcctualPageSize; aIndex++) {
            aValuesArray.add(aStartValue++);
            aValuesRange--;
        }
    }
}
